package com.vtmer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtmer.entity.IntervStatus;
import com.vtmer.entity.Progress;
import com.vtmer.entity.RegistrationForm;
import com.vtmer.mapper.ProgressMapper;
import com.vtmer.mapper.RegistrationFormMapper;
import com.vtmer.service.ProgressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Xinjie
 * @create 2022/9/26 19:02
 */
@Service
@Slf4j
public class ProgressServiceImpl extends ServiceImpl<ProgressMapper, Progress> implements ProgressService {
    @Resource
    private ProgressMapper progressMapper;

    private LambdaUpdateWrapper<Progress> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

    @Resource
    private RegistrationFormMapper registrationFormMapper;


    @Override
    public void insertProgress(Progress progress) {
        progressMapper.insert(progress);
    }

    @Override
    public Progress getProgressByUid(int uid) {
        return getOne(new QueryWrapper<Progress>().eq("progress_user_id", uid));
    }

    public Progress getProgressByStudId(long studId) {
        return getOne(new QueryWrapper<Progress>().eq("stud_id", studId));
    }

    @Override
    public List<Progress> getProgressList() {
        return progressMapper.selectList(null).size() == 0 ? null : progressMapper.selectList(null);
    }

    @Override
    public List<Progress> getProgressListByGroups(String groups) {
        List<String> strings = new ArrayList<>();
        List<RegistrationForm> registrationForms = registrationFormMapper.selectList(new QueryWrapper<RegistrationForm>().eq("interest_groups", groups));
        for (RegistrationForm registrationForm : registrationForms) {
            strings.add(registrationForm.getStudId());
        }
        List<Progress> progresses = new ArrayList<>();
        for (String string : strings) {
            progresses.add(getOne(new QueryWrapper<Progress>().eq("stud_id", string)));
        }
        return progresses.size() == 0 ? null : progresses;
    }

    @Override
    public Progress updateIntervStatusByStudId(String studId) {
        return updateAssessmResult(studId, 1);
    }

    @Override
    public IntervStatus updateIntervStatusByStudId(String studId, int state) {
        Progress progress = updateAssessmResult(studId, state);
        registrationFormMapper.update(null, new UpdateWrapper<RegistrationForm>()
                .eq("stud_id", studId)
                .set("state", state));
        return new IntervStatus(progress);
    }

    @Override
    public Progress updateIntervResultByStudId(String studId) {
        return updateAssessmResult(studId, 2);

    }

    @Override
    public Progress updateFirAssessmResultByStudId(String studId) {
        return updateAssessmResult(studId, 3);
    }

    @Override
    public Progress updateSecAssessmResultByStudId(String studId) {
        return updateAssessmResult(studId, 4);
    }

    @Override
    public Progress updateFailAssessmResultByStudId(String studId, int fail) {
        int state = fail == 1 ? 0 : 1;
        return updateFailResult(studId, state, fail);
    }


    private Progress updateAssessmResult(String studId, int state) {
        lambdaUpdateWrapper.eq(Progress::getStudId, studId)
                .set(Progress::getState, state);
        progressMapper.update(null, lambdaUpdateWrapper);
        lambdaUpdateWrapper.clear();
        return getOne(new QueryWrapper<Progress>().eq("stud_id", studId));
    }

    private Progress updateFailResult(String studId, int state, int fail) {

        log.info("调用更新淘汰数据，studId为：{}，state为：{}，fail为：{}", studId, state, fail);

        lambdaUpdateWrapper.eq(Progress::getStudId, studId)
                .set(Progress::getFail, fail);
        //更新 progress 中用户的状态

        log.info("更新流程表中用户状态");
        progressMapper.update(null, lambdaUpdateWrapper);
        lambdaUpdateWrapper.clear();


        //更新 register表中的用户报名状态
        log.info("更新报名表中用户状态");
        registrationFormMapper.update(null, new UpdateWrapper<RegistrationForm>()
                .eq("stud_id", studId).set("fail", fail));

        Progress progress = getOne(new QueryWrapper<Progress>().eq("stud_id", studId));

        log.info("更新完成，用户数据为：{}", progress);
        return progress;
    }

}
